package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.xhnews.bo.NewsBo;
import org.xhnews.to.NewsChannel;
import org.xhnews.to.NewsItem;

public class NewsListScreen extends MainScreen{
	private NewsChannel _newschannel;
	private Vector _newslist;
	private ListField _list;
	private TitleField _tf;
	private VerticalFieldManager _fieldManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK);
	Bitmap readPic = Bitmap.getBitmapResource("read.png");
	Bitmap favorPic = Bitmap.getBitmapResource("favor.png");
	Bitmap focusPic = Bitmap.getBitmapResource("focus.png");
	public NewsListScreen(){	
		super(net.rim.device.api.ui.Manager.NO_VERTICAL_SCROLL);
		_tf=new TitleField();
		add(_tf);
		_newslist=new Vector(); 
		ListCallback _callback = new ListCallback();
		_list = new ListField();
		_list.setRowHeight(getFont().getHeight()*2);
		_list.setCallback(_callback);
		add(_fieldManager);
		_fieldManager.add(_list);
		addMenuItem(_favor);
		addMenuItem(_forward);
	}
    public String getSelectedNewsId(){
    	return ((NewsItem)_newslist.elementAt(_list.getSelectedIndex())).getID();
    }
	public void initialize(NewsChannel newschannel)
	{	_newschannel=newschannel;	
		_tf.setText(newschannel.getTitle());
	    reloadList();
	}
	private void reloadList()
	{ 
		_newslist = NewsBo.getNewsByChannel(_newschannel.getFileName());
		_list.setSize(_newslist.size());
	}
	private class ListCallback implements ListFieldCallback 
	{
	    public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
	    { 
			NewsItem temp = (NewsItem) _newslist.elementAt(index);
			String title = temp.getTitle();
			String pubDate = temp.getPubDate();
			if (!temp.isRead())
				g.drawBitmap(0, y + 2, 3, list.getRowHeight() - 2, readPic, 0, 0);

			g.drawText(title, 4, y, 0, w);

			if (temp.isFavorite())
				g.drawBitmap(w - favorPic.getWidth(), y, favorPic.getWidth(),
						favorPic.getHeight(), favorPic, 0, 0);
			g.setColor(Color.GRAY);
			g.drawText(pubDate + " ", 4, y + g.getFont().getHeight() + 2,
					DrawStyle.RIGHT, w);
			g.drawLine(4, y + g.getFont().getHeight()*2-1, getWidth(), y + g.getFont().getHeight()*2-1);
			g.setColor(Color.BLACK);
	    } 
	    public Object get(ListField list, int index) 
	    {
	        return _newslist.elementAt(index); 
	    } 
	    public int indexOfList(ListField list, String prefix, int string) 
	    { 
	        return _newslist.indexOf(prefix, string); 
	    } 
	    public int getPreferredWidth(ListField list) 
	    { 
	        return Display.getWidth(); 
	    } 
	}
	private void navigationToNewsViewScreen() {
		NewsItem newsitem = (NewsItem) _newslist.elementAt(_list
				.getSelectedIndex());
		String html = NewsBo.getNewsItemContent(newsitem.getID());
		NewsBo.setItemRead(getSelectedNewsId());		
		NewsViewScreen screen = new NewsViewScreen();
		screen.initialize(html);
		reloadList();
		UiApplication.getUiApplication().pushScreen(screen);
	}

	protected boolean navigationClick(int status, int time) {
		navigationToNewsViewScreen();
		return true;
	}

	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			navigationToNewsViewScreen();
		}else if(key == Characters.ESCAPE){
			onClose();
		}
		return true;
	}
	public MenuItem _favor = new MenuItem("收藏", 110, 10) {
		public void run() {			
			NewsBo.setItemFavor(getSelectedNewsId());
			reloadList();
		}
	};
	public MenuItem _forward = new MenuItem("转发", 110, 10) {
		public void run() {						
			UiApplication.getUiApplication().pushScreen(new ForwardScreen());
		}
	};
    
}
